package com.bwhiting.display.utils
{
	import flash.display.DisplayObject;
	import flash.display.Stage;
	
	
	public function align(element:DisplayObject, parent:* = null, align:Array = null, offset:Array = null):void
	{
		if(align == null) align = [0.5,0.5];
		if(parent == null) parent = element.parent;
		var widthProperty:String = "width";
		var heightProperty:String = "height";
		var parentWidthProperty:* = "width";
		var parentHeightProperty:* = "height";		
		if(parent is Stage)
		{
			parentWidthProperty = "stageWidth";
			parentHeightProperty = "stageHeight";
		}else if(parent is Array)
		{
			parentWidthProperty = 0;
			parentHeightProperty = 1;
		}		
		element.x = (parent[parentWidthProperty] - element[widthProperty]) * align[0];
		element.y = (parent[parentHeightProperty] - element[heightProperty]) * align[1];		
		if(offset != null)
		{
			element.x += offset[0];
			element.y += offset[1];
		}
	}
}